package cn.gson.crm.model.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.gson.crm.model.Jdbc;
import cn.gson.crm.model.po.CooperationPo;
import cn.gson.crm.model.po.CooperationsPo;

public class CooperationDao {
	/**
	 * 根据员工id获取协作的学员
	 * 
	 * @param type
	 * @return
	 * @throws SQLException
	 */
	public List<CooperationPo> getCooperationList(String staffId) throws SQLException {
		List<Map<String, Object>> listData = null;
		String sql = "select cooperation.cooperation_id, cooperation.cooperation_time,cooperation.cooperation_staff_id ,cooperation.cooperation_student_id,student.student_name AS cooperation_student_name "
				+ " from cooperation LEFT OUTER JOIN student ON cooperation.cooperation_student_id = student.student_id WHERE cooperation.cooperation_staff_id = ? ";
		listData = Jdbc.getInstace().executeQueryForList(sql,staffId);
		List<CooperationPo> cooperationPoList = null;
		if (listData != null && listData.size() > 0) {
			cooperationPoList = new ArrayList<>();
			for (Map<String, Object> data : listData) {
				String s = data.get("cooperation_time").toString().split(" ")[0];
				cooperationPoList.add(
						new CooperationPo(
						type(data.get("cooperation_id")),
						type(data.get("cooperation_student_id")),
						type(data.get("cooperation_staff_id")),
						java.sql.Date.valueOf(s),
						data.get("cooperation_student_name").toString()));
			}
		}
		return cooperationPoList;
	}
	
	public List<CooperationsPo> getCooperationsList(String studentid) throws SQLException{
		String sql = "SELECT cooperation.cooperation_staff_id as id, staff.staff_name as name FROM cooperation "
				+ "LEFT OUTER JOIN staff ON staff.staff_id = cooperation.cooperation_staff_id "
				+ "WHERE cooperation_student_id = ?";
		 List<Map<String, Object>> listData = null;
		 listData = Jdbc.getInstace().executeQueryForList(sql, studentid);
		 List<CooperationsPo> list = null;
		 if(listData != null && listData.size() > 0 ){
			 list = new ArrayList<>();
			 for (Map<String, Object> data : listData) {
			 list.add(new CooperationsPo(
					 (String)data.get("name"),
					 type(data.get("id"))
					 ));
		 }
		 }
		 
		 return list;
	}
	/**
	 * 查询是否该咨询师已经协作改学生
	 * */
	public boolean getCooperation(String staffId, String stuId) throws SQLException{
		boolean type = true;
		String sql = "select * from cooperation where cooperation_student_id = ? and cooperation_staff_id = ?";
		Map<String, Object> map = Jdbc.getInstace().executeQuery(sql,stuId,staffId);
		if(map!=null){
			type =  false;
		}
		return type;
	}
	
	/**
	 * 插入协作
	 * @throws SQLException 
	 * */
	
	public boolean  insertCooperation(String staffId, String stuId) throws SQLException{
		String sql = "insert into cooperation(cooperation_student_id,cooperation_staff_id) value(?,?) ";
		return Jdbc.getInstace().executeUpdate(sql,stuId,staffId);
	}
	/**
	 * 字符串转为int
	 * */
	protected int type(Object object) {
		return Integer.parseInt(object.toString());
	}
}
